Imports System.Net
Imports System.IO
Imports System.Text


Public Class ThreeDCommuneDownloader
    Inherits BaseDownloader

    Friend Const BASE_PAGE_URL As String = "http://www.3dcommune.com/3d/store/more_info.mv?P{0}"
    Private Const IMAGE_URL As String = "http://www.3dcommune.com/3d/store/_pics/P{0}_2.jpg"

    Private Const READ_ME_URL As String = "http://www.3dcommune.com/3d/store/readme.mv?P{0}"

    Private Const URL_TO_REMOVE As String = "http://www.3dcommune.com/3d/store/more_info.mv?P"

    Private Const FIRST_ANCHOR_MATCH As String = "<form target=""wishlist"" action=""wishlist.php"" method=""post"">"
    Private Const SECOND_ANCHOR_MATCH As String = "<td width=""480"" align=""center"">"

    Private Const PRODUCT_IMAGE_START_MATCH As String = "<img src=""images/products/"
    Private Const PRODUCT_IMAGE_END_MATCH As String = """ border=""0"" align=""right"" hspace=""8"" vspace=""8"">"

    Private Const PRICE_START_MATCH As String = "<b>Price:</b>"
    Private Const PRICE_END_MATCH As String = "<br>"

    Private Const STRIKE_START As String = "<strike>"
    Private Const STRIKE_END As String = "</strike>"

    Private Const PRODUCT_NAME_START_MATCH As String = "<b>Title:</b>"
    Private Const PRODUCT_NAME_END_MATCH As String = "&nbsp; &nbsp; &nbsp; <b>"

    Private Const ARTIST_START_MATCH As String = "<tr><td align=left><span class=""genbig"">by <a href="""
    Private Const ARTISTS_START_END_MATCH As String = """ >"
    Private Const ARTIST_END_MATCH As String = "</a><br/>"

    Private Const REQUIREMENTS_START_MATCH As String = "<br /><b>Required Products:</b>"
    Private Const REQUIREMENTS_START_END_MATCH As String = "<br /><li>"
    Private Const REQUIREMENTS_END_MATCH As String = "<br /></span>"

    Public Const CODE_BASE As String = "P"

    Public Const LINK_FILENAME As String = "3DCLinks.xml"

    Public Overrides ReadOnly Property BaseImageUrl() As String
        Get
            Return IMAGE_URL
        End Get
    End Property

    Public Overrides ReadOnly Property BasePageUrl() As String
        Get
            Return BASE_PAGE_URL
        End Get
    End Property

    Protected Overrides Sub DownLoadPage(ByVal itemId As Integer)

    End Sub

    Public Overrides ReadOnly Property LinkFilename() As String
        Get
            Return LINK_FILENAME
        End Get
    End Property

    Public Overrides ReadOnly Property UrlToRemove() As String
        Get
            Return URL_TO_REMOVE
        End Get
    End Property

    Public Sub New(ByVal imageDirectory As String, _
                   ByVal imageHolder As PictureBox)
        MyBase.New(imageDirectory, _
                    imageHolder)
    End Sub

    Public Shared Function GetReadMe(ByVal productCode As String) As String

        Dim readMeText As String = ""

        Try

            productCode = productCode.ToUpper.Replace(CODE_BASE, EMPTY)

        Catch ex As Exception
            Throw New InvalidProductUrlException
        End Try

        Dim request As HttpWebRequest = CType(WebRequest.Create(String.Format(READ_ME_URL, productCode)), _
                                                  HttpWebRequest)
        request.KeepAlive = False

        Dim response As HttpWebResponse = CType(request.GetResponse(), _
                                                HttpWebResponse)

        Dim receiveStream As Stream = response.GetResponseStream()
        Dim readStream As New StreamReader(receiveStream, _
                                           Encoding.UTF8)

        Dim pageContents As String = readStream.ReadToEnd

        readStream.Close()
        response.Close()

        Return pageContents

    End Function

    Public Overrides Sub Download()

        'Try

        '    Dim Min3DCommuneId As Integer = _linkFile.MaxId + 1

        '    Download(Min3DCommuneId, _
        '             My.Settings.MaxPoserProsId)

        'Catch ex As Exception

        'End Try
        '_linkFile.WriteFile(LINK_FILENAME)

    End Sub

    Public Overrides Function DownloadImage(ByVal productCode As String, _
                                            ByVal productUrl As String) As String

        Dim productId As Integer = 0

        productId = CType(productCode.ToUpper.Replace(CODE_BASE, EMPTY), Integer)

        DownloadImage(productId, _
                      productCode)

        Return CODE_BASE & productId & JPEG_EXTENSION

    End Function

    Protected Overrides Sub DownLoadImage(ByVal imageId As Integer, _
                                            ByVal productCode As String)


        Try
            Dim fileName As String = My.Settings.ImageDirectory
            fileName &= CODE_BASE
            fileName &= imageId.ToString
            fileName &= JPEG_EXTENSION

            'Dim imageName As String = imageId.ToString
            'Select Case imageId
            '    Case Is < 100
            '        imageName = "0000" & imageName
            '    Case Is < 1000
            '        imageName = "000" & imageName
            '    Case Is < 10000
            '        imageName = "00" & imageName
            'End Select

            _imageHolder.Load(String.Format(BaseImageUrl, imageId))
            _imageHolder.Image.Save(fileName, _
                                    System.Drawing.Imaging.ImageFormat.Jpeg)
            Application.DoEvents()
        Catch ex As System.Net.WebException
            If ex.Status <> WebExceptionStatus.ProtocolError Then
                MsgBox(ex.Message)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally

        End Try

    End Sub

End Class
